package priv.bruce.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import priv.bruce.entry.UserInfo;
import priv.bruce.service.UserService;


public class ResetPassword extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private UserService userInfo = new UserService();

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String mail=request.getParameter("mail");		
		String phone=request.getParameter("phone");
		String username=request.getParameter("name");
		String newpassword=request.getParameter("password");
		
		System.out.println(newpassword);
		try {
			System.out.println(username);
			UserInfo user=userInfo.validUserInfo(username);
			
			System.out.println(user.getUser_name());
			if (!user.getUser_name().equals(username)) {
				response.getWriter().append(JSON.toJSONString(new ReturnResult(2,"修改密码失败,用户不存在",null)));
			System.out.println("aaa");
			}
			//判断所填写信息是否完全符合
			else if (user.getUser_mail().equals(mail)&&user.getUser_name().equals(username)&&user.getUser_phone().equals(phone)) {
				//调用重设密码的方法
				int changeRows=userInfo.resetPassword(phone, newpassword);
				response.getWriter().append(JSON.toJSONString(new ReturnResult(1,"修改密码成功",null)));
				System.out.println("受影响行数"+changeRows);
			}else {
				response.getWriter().append(JSON.toJSONString(new ReturnResult(2,"修改密码失败",null)));
			System.out.println("修改失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
